package com.reading.core.crawler.process;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * @author guwenqi_1995@163.com
 * @projectName betube
 * @description: TODO
 * @date 2019/8/4 11:26
 */
@Slf4j
public class MovieProcess {

    private static final String DOMAIN_NAME = "https://www.99dianying.com";

    private static final String MOVIE_URL = "/list/dianying_jingsong____.html";

    public static void processBaiduHot(Document doc){

        //提取数据
        Elements divsBig = doc.getElementsByClass("col-md-2 col-sm-3 col-xs-4");
        for(int i=0;i<divsBig.size();i++){

            String content = divsBig.get(i).toString();
            String playUrl = content.substring(content.indexOf("href=\"") + 6, content.indexOf("\" title="));

            System.out.println(playUrl);
            getMovieInfo(playUrl);
        }
    }

    private static void getMovieInfo(String playUrl) {
        try {
            Document doc = Jsoup.connect(DOMAIN_NAME + playUrl).get();
            Elements imgs = doc.getElementsByClass("video-pic");
            Elements movieInfo = doc.getElementsByClass("info clearfix");
            System.out.println("images : " + imgs.get(0).toString());
//            System.out.println("movie info : " + movieInfo.get(0).toString());
            System.out.println("************************");

            Elements infos = movieInfo.get(0).getElementsByTag("li");

//            String pixel = infos.get(2).text().substring(3);
//            String label = infos.get(3).text().substring(3);
//            String starring = infos.get(4).text().substring(3);
//            String createDate = infos.get(5).text().substring(3);

            for (Element info : infos) {
//                String text = info.text();
                String html = info.text();
                log.info("info : {}", html);
            }

            System.out.println("************************");
            System.out.println("-----------------------------------------");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String url = DOMAIN_NAME + MOVIE_URL;
        try {
            Document doc = Jsoup.connect(url).get();
            processBaiduHot(doc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
